import { defineStore } from 'pinia';
import { getNowDate } from '@/utils/helper';
interface cartState {
	cartList : any;
	cartNum : any;
	cartTags : string;
	curOrderType : any;
	tcsCarList : any;
	tcsMoreDetail : any;
	manyBuyingParams:any;
}
// 示例
export const useCartStore = defineStore('cart', {
	state:(): cartState => ({
		cartList: [],
		cartNum: 0,
		// costPlan: '',//'0'堂食,'1'外卖
		cartTags:'',
		curOrderType: {
			time: {
				dateString: '',
				starTime: '',
				endTime: '',
				timezh:'',
				timestr:'',
				textstr:'',
				fullTime:'',
				fast:false
			},
			type: ''
		},
		tcsCarList:[],//单套餐当前添加购物车商品列表
		tcsMoreDetail:{
			source:'',
			changeGoupId:'',
			type:'',
			baseMoney:0,
			cart:[],
			skuGroup:{}
		},
		manyBuyingParams:{}
	}),
	getters: {
		getCurTimes: (state) => {
			const ndate = getNowDate();
			if(ndate.fullTime&&ndate.fullTime>state.curOrderType.time.fullTime){
			}
			console.log('getCurOrderType',ndate);
			// const r = {}
			return state.curOrderType
		},
		getCartList() {
			return this.cartList;
		},
		getCurOrderTime: (state) => {
			let t :any={time:'',name:'',over:false,type:state.curOrderType.type,fast:false}
			const ndate = getNowDate();
			if(ndate.fullTime&&ndate.fullTime>state.curOrderType.time.fullTime){
				t.over = true;
			}
			// console.log('object333',state.curOrderType.time.starTime,state.curOrderType.time.starTime.split(':'));
			if(state.curOrderType.time.dateString&&state.curOrderType.time.starTime){
				let starTime:any = state.curOrderType.time.starTime||'';
				starTime = starTime.split(':');
				t.time = state.curOrderType.time.dateString + starTime.join('') + '00';
				t.name = state.curOrderType.time.textstr
				t.fast = state.curOrderType.time.fast
			}
			
			console.log('getCurOrderTime+',t);
			return t
		},
		
	},
	actions: {
		setCartList(arr : any) {
			this.cartList = arr;
			console.log('setCartList-store', arr, this.cartList);
		},
		setCartNum(nums : number) {
			this.cartNum = nums;
		},
		setCostPlan(costPlan : any) {
			this.costPlan = costPlan;
		},
		setCurOrderType(curOrderType : any) {
			this.curOrderType = curOrderType;
		},
		setCurOrderTypeString(curOrderType : any) {
			this.curOrderType.type = curOrderType;
			// this.setCurOrderTimeString({})
		},
		setCurOrderDateString(dateString : any) {
			this.curOrderType.time.dateString = dateString;
		},
		setCurOrderTimeString(v : any){
			this.curOrderType.time.starTime = v.startTime||'';
			this.curOrderType.time.endTime = v.endTime||'';
			this.curOrderType.time.timezh = v.timezh||'';
			this.curOrderType.time.timestr = v.timestr||'';
			this.curOrderType.time.textstr = v.textstr||'';
			this.curOrderType.time.fullTime = v.fullTime||'';
			this.curOrderType.time.fast = v.fast||false;
			
			// this.curOrderType.time.dateString = v.dateString||'';
		},
		setCartTags(tag:any){
			this.cartTags = tag
		},
		//单套餐-储存购物车列表-更多
		setTsCarList(arr:any=[]){
			this.tcsCarList = arr
		},
		setTmoreDetail(obj:any={}){
			this.tcsMoreDetail = obj
		},
		setManyParams(param:any={}){
			this.manyBuyingParams = param
		}
		
	},
	persist: {
		storage: {
			getItem: uni.getStorageSync,
			setItem: uni.setStorageSync
		},
		key: 'cartInfo',//本地存储key值,'tcsCarList'
		paths: ['cartNum', 'cartList', 'costPlan', 'curOrderType']// 指定持久化的数据，不写默认持久化整个state
	}
});